package Ppackage;

import java.util.Random;

public class Producer implements Runnable
{
    private Buffer buffer;
    private Random random = new Random();

    public Producer(Buffer buffer)
    {
        this.buffer = buffer;
    }

    public void run()
    {
        while (true)
        {
            try
            {
                int count = random.nextInt(4) + 1; // 随机生成1到4个产品
                buffer.produce(count);
                Thread.sleep((int) (Math.random() * 1000)); // 模拟生产时间
            }
            catch (InterruptedException e)
            {
                Thread.currentThread().interrupt();
            }
        }
    }
}
